package com.xy.pdf.controller;

import cn.hutool.core.io.IoUtil;
import com.xy.common.util.Html2PdfUtils;
import com.xy.common.util.HttpUtils;
import com.xy.dto.PdfConvertDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileItem;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import java.io.IOException;


/**
 * @author xiao.yi
 * @date 2023/9/28 13:59
 */
@Slf4j
@RestController
@RequestMapping("/pdf/convert")
public class PdfConvertController {

    @PostMapping("/html2pdf")
    public void html2pdf(@RequestBody PdfConvertDto pdfConvertDto) throws IOException {
        FileItem fileItem = Html2PdfUtils.convertPdfFromText(pdfConvertDto.getText());
        ServletOutputStream httpOutputStream = HttpUtils.getHttpOutputStream("a.pdf");
        IoUtil.copy(fileItem.getInputStream(), httpOutputStream);
        httpOutputStream.flush();
    }


}
